package com.xbai.spark.recall.engine.training

import org.apache.spark.sql.{DataFrame, SparkSession}

/**
  * @author xbai
  * @Date 2021/1/23
  */
class ItemCFTraining {

  /**
    * 物品召回训练
    *
    * @param data  userId-itemId-rating
    * @param spark spark环境
    * @return itemSimilar: itemId-itemId1-itemsim
    */
  def trainingEngine(data: DataFrame, spark: SparkSession): DataFrame = {
    val itemCF = new ItemCF
    val itemSqrtRatingSum: DataFrame = itemCF.getItemSqrtRatingSum(data, spark)
    val itemSameUserRating: DataFrame = itemCF.getItemSameUserProductSum(data, spark)
    val itemSimilar: DataFrame = itemCF.itemSimilarity(itemSqrtRatingSum, itemSameUserRating, spark)
    itemSimilar.write.mode("overwrite").saveAsTable("itemSimilar")
    itemSimilar
  }

  /**
    * 基于jaccard距离的物品召回
    *
    * @param data userId-itemId-rating
    * @param spark spark环境
    * @return itemSimilar: itemId-itemId1-itemsim
    */
  def trainEngineWithJaccard(data: DataFrame, spark: SparkSession): DataFrame = {
    val itemCF = new ItemCF
    val itemSimilar: DataFrame = itemCF.itemJaccardSimilarity(data, spark)
    itemSimilar
  }
}
